﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VgoCardChange.Models.DAL;
using VgoCardChange.Models.Objects;
using System.Net.Mail;
using System.Net;

namespace VgoCardChange.Models
{
    public class ItemRelayModels
    {
        public void CreateItemRelay(ItemRelay itemrelay)
        {
            ConnectDB con = new ConnectDB();
            try
            {
                string sql = "sp_CreateItemReplay @ItemID=N'" + itemrelay.ItemID + "'" + ",@Quantity='" + itemrelay.Quantity + "'" + ",@IsBind='" + itemrelay.IsBind + "'" + ",@UserID='" + itemrelay.UserID + "'" + ",@ServerID='" + itemrelay.ServerID + "'" + ",@GuildID='"+itemrelay.GuildID+"'";
                con.ExeNonQuery(sql);
            }
            catch (Exception ex)
            {
                Console.Write(ex);
            }
            finally
            {
                con.CloseData();
                con = null;
            }
        }
        public string SendMailFull(string mailFrom, string mailpass, string host, string port, string mailTo, string subject, string content, bool enableSsl)
        {
            try
            {
                var msg = new MailMessage
                {
                    IsBodyHtml = true,
                    Body = content,
                    From = new MailAddress(mailFrom, mailFrom)
                };
                msg.To.Add(new MailAddress(mailTo));
                msg.Subject = subject;

                var client = new SmtpClient(host, int.Parse(port))
                {
                    Credentials =
                        new NetworkCredential(mailFrom, mailpass),
                    EnableSsl = enableSsl
                };

                client.Send(msg);

                return "";
            }
            finally
            { }
        }
        public List<User> GetUser()
        {
            List<User> listuser = new List<User>();
            ConnectDB con=new ConnectDB();
            try
            {
                string sql = "select*from TblUser where RoleID!=1";
                con.ExeReader(sql);
                while (con.dataRead.Read())
                {
                    User Us = new User();
                    Us.UserID = int.Parse(con.dataRead["UserID"].ToString());
                    Us.UserName = con.dataRead["UserName"].ToString();
                    listuser.Add(Us);
                }

            }
            catch (Exception ex)
            { Console.Write(ex); }
            finally { con.CloseData(); con = null; }


            return listuser;
        }
        public List<Items> GetItem()
        {
            List<Items> listitem = new List<Items>();
            ConnectDB con = new ConnectDB();
            try
            {
                string sql = "select*from TblItems";
                con.ExeReader(sql);
                while (con.dataRead.Read())
                {
                    Items It = new Items();
                    It.ItemID = int.Parse(con.dataRead["ItemID"].ToString());
                    It.ItemName = con.dataRead["ItemName"].ToString();
                    listitem.Add(It);

                }

            }
            catch (Exception ex)
            { Console.Write(ex); }
            finally { con.CloseData(); con = null; }
            return listitem;
        }
        public string GetEmail(int UserID)
        {
            string email = "";
            ConnectDB objData = new ConnectDB();
            string strSQL = "";
            try
            {
                strSQL = "sp_getEmail @UserID=" + UserID + "";
                objData.ExeReader(strSQL);
                if (objData.dataRead.Read())
                {
                    email = objData.dataRead["Email"].ToString();
                    return email;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);

            }
            finally
            {
                objData.CloseData();
                objData = null;
            }
            return email;
        
        }
     
    }
}